<?php
if( !defined('IN_INDEX') )
{
	die('This page is hidden.');
}
if(!checkSession())
{
			function redirect($url)
			{
			?>
				<script type="text/javascript">
					{
					window.location = "<?=$url?>";
					}
				</script>
			<?php 
			}
			redirect("index.php?action=login");
	exit;
}

	
include('inc/functions_blog.php');
?>
<center>
<h1><?php echo translate('search'); ?></h1>
<br />
<br />

<form action="index.php?action=search" method="GET">
<label for="query"> Search </label>
<?php
   echo '<input name="query" value="' . $_GET['query'] . '">';
?>
<input type="submit" value="Search">
</form>
<?php
   $search_term = $_GET['query'];
   if ($search_term != "") {
     // Build search request.
     $base_url = "http://boss.yahooapis.com/ysearch/";
     $vertical = "web/";
     $version = "v1/";
     $search_term = urlencode($search_term);
     $appid = "?appid=" . "WbvTchnV34EEO0ohhvYNwVTxKQu26YiIgWOHBo69Yl5SucWFMxkYqncqVW.VQu9dsU8FGcGOrCHOhdPxXJXeKieMugWPALA-"; // replace with your app-id
     $start = "&start=" . "0";
     $count = "&count=" . "10";
     $request_url = $base_url . $vertical . $version . $search_term . $appid . $start . $count;
     // Send search request.
     $curl_handle = curl_init($request_url);
     curl_setopt($curl_handle,CURLOPT_URL, $request_url);
     curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 5);
     $raw_results = curl_exec($curl_handle);
     curl_close($curl_handle);
     $results_dict = json_decode($raw_results);
     $results = $results_dict->ysearchresponse->resultset_web;
   }
   else {
     $results = array();
   }
?>
<div id="results">
     <?php
     foreach ($results as $result) {
     echo '<div class="result">';
     echo '<h3 class="title"><a href='.$result->clickurl.'">'.$result->title.'</a></h3>';
     echo '<p class="summary">'.$result->abstract.'</p>';
     echo '</div>';
   }
  ?>
</div>
 

</center>